package com.phpxiu.app.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.phpxiu.app.api.OnDialogClickListener;
import com.phpxiu.app.config.Constants;
import com.phpxiu.app.config.HttpConfig;
import com.phpxiu.app.controller.avcontroller.QavsdkControl;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.MySelfInfo;
import com.phpxiu.app.okhttp.OKHttp;
import com.phpxiu.app.okhttp.OKHttpResponseModel;
import com.phpxiu.app.okhttp.handler.OKHttpUIHandler;
import com.phpxiu.app.okhttp.param.OKHttpParam;
import com.phpxiu.app.utils.KKYUtil;
import com.tencent.TIMCallBack;
import com.tencent.TIMManager;


/**
 * Created by 吴金洲 on 2016/7/18
 */
public class ForceLoginOut extends UIBase {

    private final static String TAG="ForceLoginOut";

    public final static String REASON_MSG="exit_msg";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String msg=getIntent().getStringExtra(REASON_MSG);
        interactiveDialog(getString(R.string.login_kick),msg,getString(R.string.login_retry_title),getString(R.string.login_out_title),new OnDialogClickListener() {
            @Override
            public void onOk() {
                KKYUtil.log("重新登录....");
                Intent login=new Intent(ForceLoginOut.this,Login.class);
                login.putExtra(BaseLogin.LOGIN_ACTION, BaseLogin.FORCE_LOG_OUT);
                ForceLoginOut.this.startActivity(login);
                finish();
            }

            @Override
            public void onCancel() {
                KKYUtil.log("退出程序..");
                sendBroadcast(new Intent(Constants.BD_FORCE_LOG_OUT));
                finish();
            }
        });
        logout(msg);
    }

    private void logout(String msg){
        //清除本地缓存
        MySelfInfo.getInstance().clearCache(this);
        //登出
        MySelfInfo.getInstance().loginOut();

        //反向初始化avsdk
        QavsdkControl.getInstance().stopContext();

        TIMManager.getInstance().logout(new TIMCallBack() {
            @Override
            public void onError(int i, String s) {
                if (KKYUtil.DEBUG) {
                    Toast.makeText(ForceLoginOut.this, "IM登出失败：" + i + "@" + s, Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onSuccess() {
                if (KKYUtil.DEBUG) {
                    Toast.makeText(ForceLoginOut.this, "IM登出成功", Toast.LENGTH_LONG).show();
                }
                logout();
            }
        });


    }

    /**
     * 退出登录
     */
    private void logout(){
        OKHttpParam param= OKHttpParam.builder(false);
        OKHttp.post(HttpConfig.API_LOGIN_OUT, param.jsonParam(), TAG, new OKHttpUIHandler(OKHttpResponseModel.class) {
            @Override
            public void onSuccess(Object model) {
                OKHttpResponseModel m = (OKHttpResponseModel) model;
                KKYUtil.log("退出登录成功结果：" + m.getResponseStr());
            }

            @Override
            public void onErr(String errMsg) {
                KKYUtil.log("退出登录失败：" + errMsg);
            }
        });
    }
}
